package com.mobilebsc.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mobilebsc.R;
import com.mobilebsc.datamanager.IDataReadyListener;
import com.mobilebsc.datamanager.PerspectivesDataManager;
import com.mobilebsc.datamanager.StrategicGoalsDataManager;
import com.mobilebsc.datatype.Perspective;
import com.mobilebsc.datatype.StrategicGoal;

public class PerspectiveActivity extends Activity implements IDataReadyListener
{
    private LinearLayout              llMain;
    private int                       indexCount  = 1;
    private DataManager               dataManager = new DataManager();
    private PerspectivesDataManager   pdm         = new PerspectivesDataManager(this);
    private StrategicGoalsDataManager sgdm        = new StrategicGoalsDataManager(this);
    private int                       perspectiveId;
    private int                       loadState;
    private Intent                    intentStrategicGoal;
    public static String              extra_message = "";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perspective);

        // Get the message from the intent
        Intent intent = getIntent();
        perspectiveId = Integer.valueOf(intent
                .getStringExtra(mobileBSCActivity.extra_message));
        
        if (perspectiveId == 0) perspectiveId = 1;
        
        Log.i("Test", Integer.toString(perspectiveId));

        llMain = (LinearLayout) findViewById(R.id.LinearLayout1);

        loadState = 0;
        pdm.getById(perspectiveId);

        intentStrategicGoal = new Intent(this, StrategicGoalActivity.class);

        TextView tvBC = (TextView) findViewById(R.id.textViewBC);
        tvBC.setText("Home > BSC > Perspective >");
    }

    private void showPerspectives()
    {
        for (Perspective p : dataManager.getPerspectives())
        {
            TextView tvPerspective = new TextView(this);

            tvPerspective.setText(p.getName());
            tvPerspective.setHint(p.getDescription());
            tvPerspective.setTextAppearance(this, R.style.mBSCPerspName);
            tvPerspective.setBackgroundColor(Color.BLUE);
            tvPerspective.setGravity(Gravity.CENTER_HORIZONTAL);
            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            llp.setMargins(0, 3, 0, 0);
            tvPerspective.setLayoutParams(llp);

            indexCount++;

            llMain.addView(tvPerspective, indexCount);

            showStrategicGoals(p.getStrategicGoals());
        }
    }

    private void showStrategicGoals(ArrayList<StrategicGoal> strategicGoals)
    {
        for (StrategicGoal sg : strategicGoals)
        {
            final int sgId = sg.getId();
            TextView tvSG = new TextView(this);

            tvSG.setText(sg.getName());
            tvSG.setHint(sg.getDescription());
            tvSG.setTextAppearance(this, R.style.mBSCSGName);
            tvSG.setBackgroundColor(Color.GREEN);
            tvSG.setPadding(5, 5, 5, 5);
            tvSG.setClickable(true);
            tvSG.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View viewIn) 
                {
                    intentStrategicGoal.putExtra(extra_message, Integer.toString(perspectiveId) + ";" + Integer.toString(sgId));
                    startActivity(intentStrategicGoal);
                }
            });

            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 3.5f);
            llp.setMargins(0, 2, 0, 0);

            tvSG.setLayoutParams(llp);

            indexCount++;

            llMain.addView(tvSG, indexCount);
        }
    }

    public void onClick_btn_Back(View view)
    {
        startActivity(new Intent(this, mobileBSCActivity.class));
    }

    public int calcBGColor(double Value)
    {
        if (Value >= 8)
        {
            return Color.rgb(220, 220, 220);
        } else if (Value >= 4)
        {
            return Color.rgb(255, 215, 0);
        } else
        {
            return Color.rgb(178, 34, 34);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.perspective, menu);
        return true;
    }

    @Override
    public void dataReady()
    {
        boolean display = false;
        
        Log.i("Test", "dataReady");
        
        if (loadState == 0)
        {
            List<Perspective> list = pdm.loadById(perspectiveId);

            dataManager.setPerspectives((ArrayList<Perspective>) list);
            
            if (list.size() > 0)
            {
                loadState = 1;
                sgdm.getByPerspectiveId(perspectiveId);
            }
        }    
        else if (loadState == 1)
        {
            List<StrategicGoal> list = sgdm.loadAll();

            dataManager.getPerspectives().get(0).setStrategicGoals(((ArrayList<StrategicGoal>) list));
            
            display = true;
        }
        
        if (display) showPerspectives();
    }
}
